home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / dos / extra / source / browser / browser.hqx / Browser / iconcontrol ƒ / iconcontrol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-28  |  7.5 KB  |  348 lines

  1. /* TODO: check for null ˜handles */
  2.  
  3. #include "iconcontrol.h"
  4. #define NDEBUG
  5. #include <assert.h>
  6. #include <Packages.h>
  7.  
  8. #include "iconcontrol.proto.h"
  9. #include "getrects.proto.h"
  10.  
  11. #define HACKY_COLOR_SAVE        \
  12.   {                    \
  13.     RGBColor save_fore, save_back;    \
  14.                         \
  15.     GetForeColor (&save_fore);        \
  16.     GetBackColor (&save_back);        \
  17.     ForeColor (blackColor);        \
  18.     BackColor (whiteColor);
  19.  
  20. #define HACKY_COLOR_RESTORE        \
  21.     RGBForeColor (&save_fore);        \
  22.     RGBBackColor (&save_back);        \
  23.   }
  24.  
  25. void
  26. drawicxx (Ptr pat, BitMap * mbm, Rect * dest, CWindowPtr w, short size, short depth)
  27. {
  28.   PixMap spm;
  29.   Rect sr;
  30.   unsigned char state;
  31.  
  32.   SetRect (&sr, 0, 0, size, size);
  33.   spm.baseAddr = pat;
  34.   spm.rowBytes = (size * depth / 8);
  35.   if (depth != 1)
  36.     spm.rowBytes |= 0x8000;
  37.   SetRect (&spm.bounds, 0, 0, size, size);
  38.   spm.pmVersion = 0;
  39.   spm.packType = 0;
  40.   spm.packSize = 0;
  41.   spm.hRes = spm.vRes = (Fixed) 72;
  42.   spm.pixelType = 0;
  43.   spm.pixelSize = depth;
  44.   spm.cmpCount = 1;
  45.   spm.cmpSize = depth;
  46.   spm.planeBytes = 0;
  47. #if 0
  48. /* TODO: figure out the correct way to get the CTabHandle */
  49.   spm.pmTable = (*w->portPixMap)->pmTable;
  50. #else
  51.   spm.pmTable = GetCTable (depth);
  52. #endif
  53.  
  54.   SetRect (&sr, 0, 0, size, size);
  55.   state = HGetState ((Handle) w->portPixMap);
  56.   HLock ((Handle) w->portPixMap);
  57.     
  58.   HACKY_COLOR_SAVE
  59.   CopyMask ((BitMap *) & spm, mbm, (BitMap *) * w->portPixMap, &sr, &sr, dest);
  60.   HACKY_COLOR_RESTORE
  61.     
  62.   
  63.   HSetState ((Handle) w->portPixMap, state);
  64. #if 1
  65.   DisposCTable (spm.pmTable);
  66. #endif /* 0 */
  67. }
  68.  
  69.  
  70. void
  71. dodrawcontrol (short varcode, ControlHandle c)
  72. {
  73.   Rect r, dr, tr;
  74.   Handle mask, data;
  75.   GrafPtr saveport;
  76.   short textsize, depth, size, view, textfont;
  77.   BitMap mbm;
  78.   item **ih;
  79.   unsigned char mstate, dstate;
  80.   Str255 s;
  81.   RgnHandle save_clip_rgnh, new_clip_rgnh;
  82. #if defined(YELLOW_HILITE)
  83.   RGBColor blendcolor, hilite_color;
  84.  
  85.   blendcolor.red = blendcolor.green = 32767;
  86.   blendcolor.blue = 65535;
  87.   hilite_color.red = hilite_color.green = 65535;
  88.   hilite_color.blue = 0;
  89. #else
  90.   RGBColor blendcolor, hilite_color;
  91.  
  92.   blendcolor.red = 45000;
  93.   blendcolor.green = 32767;
  94.   blendcolor.blue = 65535;
  95.   hilite_color.red = 0;
  96.   hilite_color.green = 65535;
  97.   hilite_color.blue = 0;
  98. #endif
  99.  
  100.   GetPort (&saveport);
  101.   SetPort ((*c)->contrlOwner);
  102.   save_clip_rgnh = NewRgn ();
  103.   new_clip_rgnh = NewRgn ();
  104.   GetClip (save_clip_rgnh);
  105.   r = (*c)->contrlOwner->portRect;
  106.   r.right -= 15;
  107.   RectRgn (new_clip_rgnh, &r);
  108.   SectRgn (new_clip_rgnh, save_clip_rgnh, new_clip_rgnh);
  109.   SetClip (new_clip_rgnh);
  110.  
  111.   ih = (item **) (*c)->contrlData;
  112.   getrects (c, &tr, &dr);
  113.   EraseRect (&tr);
  114.   InsetRect (&dr, -1, -1);
  115.   EraseRect (&dr);
  116.   InsetRect (&dr, 1, 1);
  117.   view = (*ih)->view;
  118.   if (view == ICSVIEW)
  119.     {
  120.       data = (*(*ih)->iconfam)->ics8;
  121.       if (data == 0 || (*(*theGDevice)->gdPMap)->pixelSize < 8)
  122.     {
  123.       data = (*(*ih)->iconfam)->ics4;
  124.       if (data == 0 || (*(*theGDevice)->gdPMap)->pixelSize < 4)
  125.         {
  126.           data = (*(*ih)->iconfam)->icssh;
  127.           depth = 1;
  128.           size = 16;
  129.         }
  130.       else
  131.         {
  132.           depth = 4;
  133.           size = 16;
  134.         }
  135.     }
  136.       else
  137.     {
  138.       depth = 8;
  139.       size = 16;
  140.     }
  141.       mask = (*(*ih)->iconfam)->icssh;
  142.       
  143.       
  144.       if (!((mask == 0) ^ (data != 0)))
  145.         DebugStr ("\pUnexpected (mask, data) values");
  146.       assert ((mask == 0) ^ (data != 0));
  147.     }
  148.   if (view == ICONVIEW || (view == ICSVIEW && data == 0))
  149.     {
  150.       data = (*(*ih)->iconfam)->icl8;
  151.       if (data == 0 || (*(*theGDevice)->gdPMap)->pixelSize < 8)
  152.     {
  153.       data = (*(*ih)->iconfam)->icl4;
  154.       if (data == 0 || (*(*theGDevice)->gdPMap)->pixelSize < 4)
  155.         {
  156.           data = (*(*ih)->iconfam)->icnsh;
  157.           if (data == 0)
  158.             DebugStr ("\pNo data?");
  159.           assert (data != 0);
  160.           depth = 1;
  161.           size = 32;
  162.         }
  163.       else
  164.         {
  165.           depth = 4;
  166.           size = 32;
  167.         }
  168.     }
  169.       else
  170.     {
  171.       depth = 8;
  172.       size = 32;
  173.     }
  174.       mask = (*(*ih)->iconfam)->icnsh;
  175.       if (mask == 0)
  176.         DebugStr ("\pmask == 0");
  177.       assert (mask != 0);
  178.     }
  179.  
  180.   if (view != LISTVIEW)
  181.     {
  182.       dstate = HGetState (data);
  183.       HLock (data);
  184.       mstate = HGetState (mask);
  185.       HLock (mask);
  186.       mbm.baseAddr = *mask + size * size / 8;
  187.       mbm.rowBytes = size / 8;
  188.       SetRect (&mbm.bounds, 0, 0, size, size);
  189.       drawicxx (*data, &mbm, &dr, (CWindowPtr) (*c)->contrlOwner, size, depth);
  190.       if ((*c)->contrlHilite == inButton)
  191.     {
  192. #if 0
  193.       CopyBits (&mbm, &(*c)->contrlOwner->portBits, &mbm.bounds, &dr, srcXor, (RgnHandle) 0);
  194. #else
  195.       PenNormal ();
  196.       HACKY_COLOR_SAVE
  197.       RGBForeColor (&hilite_color);
  198.       PenMode (blend);
  199.       OpColor (&blendcolor);
  200.       PaintOval (&dr);
  201.       PenNormal ();
  202.       HACKY_COLOR_RESTORE
  203. #endif
  204.     }
  205.       else if ((*c)->contrlHilite == INSELECTEDICON)
  206.     {
  207. #if 0
  208.       OpColor (halfwhite);
  209.       hiliteMode &= ~(1 << hiliteBit);
  210.       InvertRect (&dr);
  211. #else
  212.       PenNormal ();
  213.       HACKY_COLOR_SAVE
  214.       RGBForeColor (&hilite_color); /* new addition */
  215.       PenMode (blend);
  216.       OpColor (&blendcolor);
  217.       PaintOval (&dr);
  218.       PenNormal ();
  219.       HACKY_COLOR_RESTORE
  220. #endif
  221.     }
  222.       HSetState (data, dstate);
  223.       HSetState (mask, mstate);
  224.     }
  225.  
  226.   textsize = (*c)->contrlOwner->txSize;
  227.   textfont = (*c)->contrlOwner->txFont;
  228.   TextSize (FONTSIZE);
  229.   TextFont (applFont);
  230. /* TODO: 8 should be figured out with getfontinfo */
  231.   if (view == LISTVIEW)
  232.     {
  233.       MoveTo (tr.left + ICSSIZE, tr.top + 8);
  234.       DrawString ((*c)->contrlTitle);
  235.       MoveTo (tr.left + SIZETEXTLEFT, tr.top + 8);
  236.       NumToString ((*ih)->size, s);
  237.       DrawString (s);
  238.       MoveTo (tr.left + DATETEXTLEFT, tr.top + 8);
  239.       IUDateString ((*ih)->moddate, abbrevDate, s);
  240.       DrawString (s);
  241.     }
  242.   else
  243.     {
  244.       MoveTo (tr.left, tr.top + 8);
  245.       DrawString ((*c)->contrlTitle);
  246.     }
  247.   TextSize (textsize);
  248.   TextFont (textfont);
  249.  
  250.   if ((*(item **) (*c)->contrlData)->selected)
  251.     {
  252.       FrameRect (&dr);
  253.       FrameRect (&tr);
  254.     }
  255.   SetClip (save_clip_rgnh);
  256.   DisposeRgn (save_clip_rgnh);
  257.   DisposeRgn (new_clip_rgnh);
  258.   SetPort (saveport);
  259. }
  260.  
  261. long
  262. dotest (long param, ControlHandle c)
  263. {
  264.   Point p;
  265.   Rect ir, tr;
  266.  
  267.   p.v = HiWord (param);
  268.   p.h = LoWord (param);
  269.   getrects (c, &tr, &ir);
  270.   if ((*c)->contrlHilite != 255)
  271.     if (PtInRect (p, &ir) || 
  272.       (PtInRect (p, &tr) && (*(item **) (*c)->contrlData)->view == LISTVIEW))
  273.       if ((*(item **) (*c)->contrlData)->selected)
  274.     return (long) INSELECTEDICON;
  275.       else
  276.     return (long) inButton;
  277.   if (PtInRect (p, &tr))
  278.     return (long) INTEXT;
  279.   return 0L;
  280. }
  281.  
  282. void
  283. docalc (RgnHandle rgn, ControlHandle c)
  284. {
  285.   RgnHandle rgn1, rgn2;
  286.   Rect ir, tr;
  287.  
  288.   rgn1 = NewRgn ();
  289.   rgn2 = NewRgn ();
  290.   getrects (c, &tr, &ir);
  291.   RectRgn (rgn1, &ir);
  292.   RectRgn (rgn2, &tr);
  293.   UnionRgn (rgn1, rgn2, rgn);
  294.   DisposeRgn (rgn1);
  295.   DisposeRgn (rgn2);
  296. }
  297.  
  298. void
  299. dodispose (ControlHandle c)
  300. {
  301.   DisposHandle ((Handle) (*c)->contrlData);
  302. }
  303.  
  304.  
  305. pascal long
  306. main (short varcode, ControlHandle c, short message, long param)
  307. {
  308.   long retval;
  309.  
  310.   retval = 0;
  311.   switch (message)
  312.     {
  313.     case drawCntl:
  314. /* Ignore param, always draw the whole control */
  315.       dodrawcontrol (varcode, c);
  316.       break;
  317.     case testCntl:
  318.       retval = dotest (param, c);
  319.       break;
  320.     case calcCRgns:
  321. #ifdef LIVEDANGEROUSLY
  322.       if (param & 0x80000000)
  323.     param &= 0x00FFFFFF
  324. #endif /* LIVEDANGEROUSLY */
  325. /* FALL THROUGH --> */
  326.     case calcCntlRgn:
  327.     docalc ((RgnHandle) param, c);
  328.       break;
  329.     case dispCntl:
  330.       dodispose (c);
  331.       break;
  332.     case autoTrack:
  333.       break;
  334.  
  335.     case initCntl:
  336.     case posCntl:
  337.     case thumbCntl:
  338.     case dragCntl:
  339.     case calcThumbRgn:
  340.       break;
  341.     default:
  342.       DebugStr ("\pUnknown icon message");
  343.       assert (0);
  344.       break;
  345.     }
  346.   return retval;
  347. }
  348.